<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./css/reset.css" />
    <title>京东轮播图练习</title>
    <style>
      .image-list {
        width: 590px;
        height: 470px;
        position: relative;
        margin: 50px auto;
      }

      .image-list li {
        position: absolute;
      }

      .image-list li a {
        display: block;
      }

      /* 通过修改元素的层级来显示指定的图片 */
      /* .image-list li:nth-child(2) {
        z-index: 1;
      } */

      .pointer {
        position: absolute;
        z-index: 999;
        left: 40px;
        bottom: 20px;
      }

      .pointer a {
        float: left; /* 设置元素左浮动，则自动变为块元素 */
        width: 10px;
        height: 10px;
        margin: 0 2px;
        border-radius: 50%;
        background-color: rgba(255, 255, 255, 0.3);
        background-clip: content-box; /* 将背景颜色设置到内容区，边框和内边距不设背景颜色 */
        border: 2px solid transparent;
        cursor: auto;
      }

      .pointer a.active {
        background-color: #fff;
        border: 2px solid rgba(255, 255, 255, 0.3);
      }
    </style>
  </head>
  <body>
    <ul class="image-list">
      <li>
        <a href="javascript:;"><img src="./images/carousel/1.jpg" /></a>
      </li>
      <li>
        <a href="javascript:;"><img src="./images/carousel/2.jpg" /></a>
      </li>
      <li>
        <a href="javascript:;"><img src="./images/carousel/3.jpg" /></a>
      </li>
      <li>
        <a href="javascript:;"><img src="./images/carousel/4.jpg" /></a>
      </li>
      <li>
        <a href="javascript:;"><img src="./images/carousel/5.jpg" /></a>
      </li>

      <div class="pointer">
        <a class="active" href="javascript:;"></a>
        <a href="javascript:;"></a>
        <a href="javascript:;"></a>
        <a href="javascript:;"></a>
        <a href="javascript:;"></a>
      </div>
    </ul>

    <script>
      const pointer = document.querySelectorAll(".pointer a");
      const imageList = document.querySelectorAll(".image-list li");

      pointer.forEach((a, index) => {
        console.log(a, index);
        a.onmouseenter = function (e) {
          const active = document.querySelector(".pointer .active");
          active.classList.remove("active");
          a.classList.add("active");
          clearZIndex();
          imageList[index].style.zIndex = 1;
        };
      });

      function clearZIndex() {
        imageList.forEach(item => {
          item.style.zIndex = "";
        });
      }
    </script>
  </body>
</html>
